package com.zan.readwrite;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @Author Zan
 * @Create 2024/3/1 12:27
 * @Description : 演示读写锁降级
 */

// 在写的时候可以进行读操作
public class Demo1 {
    public static void main(String[] args) {
        // 可重入读写锁对象
        ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();

        // 创建读锁
        ReentrantReadWriteLock.ReadLock readLock = rwLock.readLock();

        // 创建写锁
        ReentrantReadWriteLock.WriteLock writeLock = rwLock.writeLock();

        // 模拟锁降级

        // 2.获取读锁
        readLock.lock();
        System.out.println("---read");

        // 1.获取写锁
        writeLock.lock();
        System.out.println("---write");

        // 3.释放写锁
//        writeLock.unlock();

        // 4.释放读锁
//        readLock.unlock();
    }
}
